<template>
  <base-breadcrumb :title="$route.meta.title" :breadcrumb="!isAudit">
    <!-- 职工维权上报编辑 -->
    <a-form-model
      ref="ruleForm"
      :model="form"
      :label-col="{ span: 6 }"
      :wrapper-col="{ span: 18 }"
      :rules="editModel != 0 ? rules : {}"
    >
      <a-spin :spinning="loading[0] || loading[1]">
        <!-- 表格 -->
        <a-card :bordered="false">
          <title-name title="基本信息"></title-name>
          <custom-form :formList="formList" :form="form" :editModel="editModel == 0 ? 'V' : ''">
            <!-- 附件上传 -->
            <template #fileInfoVOS>
              <template v-if="editModel != 0">
                <l-table businessCode="UNION6" :uploadArrys.sync="form.fileInfoVOS"></l-table>
                <div class="file_prompt">支持扩展名：.rar .zip .doc .docx .pdf .jpg...</div>
              </template>
              <file-link v-else :fileList="form.fileInfoVOS" style="margin-top: -3px"></file-link>
            </template>
          </custom-form>
        </a-card>
        <a-card v-if="form.replyOpinion" :bordered="false">
          <title-name title="回复"></title-name>
          <a-form-model-item prop="replyOpinion" label="意见" class="requiredFormItem">
            <div style="margin-top: -3px">
              {{ form.replyOpinion || '无' }}
            </div>
          </a-form-model-item>
          <a-form-model-item prop="replyFileInfoVOS" label="附件" class="requiredFormItem">
            <file-link :fileList="form.replyFileInfoVOS" style="margin-top: -3px"></file-link>
          </a-form-model-item>
        </a-card>
      </a-spin>
      <!-- 页脚操作栏 -->
      <div v-if="!isConsultation" class="btn_box page-btn-right-top">
        <a-button
          :loading="loading[0]"
          :disabled="loading[1]"
          v-if="(editModel == 1 && (statusCode == '0' || statusCode == '3')) || editModel == 2"
          type="primary"
          class="button_right"
          @click="handleSubmit(0)"
          >{{ statusCode == '2' || statusCode == '3' ? '保存' : '保存草稿' }}</a-button
        >
        <a-button
          :loading="loading[1]"
          :disabled="loading[0]"
          v-if="
            (editModel == 1 && (statusCode == '0' || statusCode == '3')) ||
            editModel == 2 ||
            (editModel != 0 && statusCode == '2')
          "
          type="primary"
          class="button_right"
          @click="handleSubmit(1)"
          >提交</a-button
        >
        <a-button type="primary" @click="handleActiveClose">关闭</a-button>
      </div>
    </a-form-model>
    <selectApprover :businessId="businessId"  :processConfig="setProcessConfig"></selectApprover>
  </base-breadcrumb>
</template>

<script>
import { workersRightsProtection } from '@/api/union/workersCongress' // api
import BaseEditPage from '@/views/baseEditPage' // 编辑详情页基础配置

export default {
    name:'a'+Date.now(),
  extends: new BaseEditPage(),
  props: {
    // 是否作为审批详情
    isAudit: {
      type: Boolean,
      default: false
    },
    // 是否作为意见征询
    isConsultation: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      loading: { 0: false, 1: false }, // 草稿和提交按钮loding
      editModel: 0, // 操作状态:0 查看;1 编辑;2 新增
      form: {
        creator: '',
        createTime: '',
      }, // 表单
      rules: {
        theme: [{ required: true, whitespace: true, message: '请输入荣誉名称', trigger: 'change' }],
        content: [{ required: true, whitespace: true, message: '请输入主要内容', trigger: 'change' }],
        fileInfoVOS: [{ type: 'array', required: true, message: '请上传佐证文件', trigger: 'blur' }],
      }, // 校验规则
      init: false, // 初始化控件
      statusCode: '', // 审批状态
      //获取流程配置值
      processConfig: null,
      //点击提交发送流程配置值
      setProcessConfig: null,
      businessId: null,
    }
  },
  watch: {
    // 监听附件实现附件校验
    'form.fileInfoVOS'() {
      if (this.editModel != 0 && this.init) {
        this.$nextTick(() => {
          this.$refs.ruleForm?.validateField('fileInfoVOS')
        })
      }
    },
  },
  async mounted() {
    this.$route.meta.title = ['职工维权上报查看', '职工维权上报编辑', '职工维权上报新增'][this.editModel]
    // 不为添加时请求详情
    if (this.editModel != 2) {
      this.loading[0] = true
      workersRightsProtection
        .getDetail({ id: this.id })
        .then((res) => {
          this.loading[0] = false
          this.form = res.data
          this.statusCode = res.data?.status
          this.$nextTick(() => {
            this.init = true
          })
        })
        .catch(() => {
          this.loading[0] = false
        })
    } else {
      this.$nextTick(() => {
        this.form.creator = this.$userInfo().name
        this.form.createTime = this.$dateFormat(new Date(), 'D')
        this.init = true
      })
    }
     //获取通过获取流程配置值
    this.processConfig = await this.$getConfigFn('union_workers_power') 
  },
  computed: {
    formList() {
      return [
        {
          keyName: 'theme',
          type: 'BaseInput',
          label: '主题',
        },
        {
          keyName: 'creator',
          type: 'a-input',
          disabled: true,
          label: '填报人',
        },
        {
          keyName: 'createTime',
          type: 'a-input',
          disabled: true,
          label: '填报时间',
        },
        {
          keyName: 'content',
          type: 'BaseTextarea',
          label: '主要内容',
          span: 24,
        },
        {
          slot: true,
          keyName: 'fileInfoVOS',
          label: '佐证材料',
          span: 24,
        },
      ]
    },
  },
  methods: {
    /**
     * 提交表单
     */
    handleSubmit(statusCode) {
      let that = this
      this.setProcessConfig = null; 
      let api = ['update', 'create'][that.editModel - 1] // 根据操作切换url地址
      // 校验表单
      that.$refs.ruleForm.validate((e) => {
        // if (e) {
        if (e || statusCode === 0) {
          // 提交表单
          statusCode === 0 && that.$refs.ruleForm.clearValidate()
          let form = that.$clone(that.form)
          // 格式化文件列表
          if (form.fileInfoVOS?.length > 0) {
            form.fileId = form.fileInfoVOS.map((item) => item.commonId).join()
          }
          // 删除多余参数
          delete form.creator
          delete form.createTime
          form.status =
            (that.statusCode == '3' && statusCode == 0) || (that.statusCode == '2' && statusCode == 1)
              ? that.statusCode
              : statusCode

            this.submitApprovalConfirm (statusCode*1 ===1 ?this.processConfig:6, async(flag) =>{
                if(!flag) {
                   that.loading[statusCode] = false
                  return
                }
            that.loading[statusCode] = true
            workersRightsProtection[api](form)
              .then((res) => {
                that.loading[statusCode] = false
                that.$message.success(['保存', '提交'][statusCode] + '成功！')
                if(statusCode*1===1){
                   this.businessId = res.data.id;
                   this.setProcessConfig = this.processConfig;
                }else{
                  that.$multiTab.close()
                }
              })
              .catch(() => {
                that.loading[statusCode] = false
              })
            })  
        }
      })
    },
  },
}
</script>

<style lang="less" scoped>
.file_prompt {
  color: #999;
}
</style>